package com.juli.school.service.utils;

import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;

import com.mysql.jdbc.Connection;

/**
 * 连接数据库(配合ImportEntry、WorkThread、ConnectionPool)
 * 
 * @author wjp
 * @date 2017年8月2日
 *
 */
public class ConnectionClass {
	private Connection con = null;
	private boolean busy;
	private static Properties config = new Properties();// 读取property配置文件
	private String user;
	private String password;
	final static String jdbcDriver = "com.mysql.jdbc.Driver";
	private String url;

	public ConnectionClass() {
		try {
			InputStream in = JdbcPool.class.getClassLoader().getResourceAsStream("config/datasource.properties");
			config.load(in);
			url = config.getProperty("school.jdbc.url");
			user = config.getProperty("school.jdbc.username");
			password = config.getProperty("school.jdbc.password");
			Class.forName(jdbcDriver);
			con = (Connection) DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 注册mysql驱动
	 * 
	 * @param url
	 * @param user
	 * @param password
	 */
	public ConnectionClass(String jdbcDriver, String url, String user, String password) {
		this.busy = false;
		try {
			Class.forName(jdbcDriver);
			con = (Connection) DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Connection getCon() {

		return con;
	}

	public boolean isBusy() {
		return busy;
	}

	public synchronized void setBusy(boolean busy) {
		this.busy = busy;
	}

}
